package com.rm.zeyou.pay.wxpay.controller;


import com.alibaba.fastjson.JSONObject;
import com.rm.zeyou.mapper.ApplicationConfigMapper;
import com.rm.zeyou.pay.wxpay.util.WxLoginUtil;
import com.rm.zeyou.pay.wxpay.util.WxPayUtil;
import com.rm.zeyou.response.ApiRestResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description: 微信支付校验controller
 * @Title: WeChatPayConfigController
 * @Package com.sxskz.software.base.pay.wxpay.controller
 * @Author ZhangKeChen  🤙🤙🏼🤙🏽
 * @Date 2023/4/28 2:27 PM
 */
@Slf4j
@RestController
@RequestMapping("/api/wechat")
public class WeChatPayH5ConfigController {
    @Resource
    private WxPayUtil wxPayUtil;
    //@Resource
    //private WxPayEntity wxPayEntity;

    @Resource
    private ApplicationConfigMapper applicationConfigMapper;
    /**
     * 支付config校验接口
     */
    @GetMapping("/payConfig")
    public ApiRestResponse payConfig(@RequestParam String url) {
        String accountAppid = applicationConfigMapper.selectOneData("account_appid");
        String accountSecret = applicationConfigMapper.selectOneData("account_secret");
        //用code获取access
        String getOpenid = "https://api.weixin.qq.com/cgi-bin/token?" +
                "appid=" +accountAppid +
                "&secret=" + accountSecret +
                "&grant_type=client_credential";
        JSONObject userInfo = WxLoginUtil.getUserInFor(getOpenid);
        log.info("用户openid信息：{}", userInfo);
        log.info("用户access_token：{}", userInfo.getString("access_token"));
        String access_token = userInfo.getString("access_token");
        String jsapiTicket = wxPayUtil.getJsapiTicket(access_token);
        return  ApiRestResponse.success(wxPayUtil.sign(jsapiTicket,url));
    }


}
